package net.mimai.vultrue.bus.excel.reader.meta;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface ExcelField {

    Logger logger = LoggerFactory.getLogger(ExcelField.class);

    int index();

    String name() default "";

    ExcelFieldType type() default ExcelFieldType.Str;

    String valid() default "";


    enum ExcelFieldType {

        Str {
            @Override
            public String buildSetString(String in) {
                return in;
            }

            @Override
            public void buildSet(Field field, Object obj, String in) throws IllegalAccessException {
                if (StringUtils.isBlank(in)) {
                    in = "";
                }
                field.set(obj, in);
            }
        }, Date {
            @Override
            public String buildSetString(String in) {
                if (StringUtils.isBlank(in)) {
                    in = "";
                }
                if (in.matches("\\d{4}-\\d{2}-\\d{2}")) {
                    return in;
                }
                if (in.matches("\\d{4}/\\d{2}/\\d{2}")) {
                    return in.replaceAll("/", "-");
                }
                if (in.matches("\\d{1,2}/\\d{1,2}/\\d{2}")) {
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy");
                    SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        return simpleDateFormat2.format(simpleDateFormat.parse(in));
                    } catch (Exception e) {
                        logger.error(String.format("未知的日期格式,日期为:%s", in));
                    }
                }
                logger.error(String.format("未知的日期格式,日期为:%s", in));
                return in;
            }

            @Override
            public void buildSet(Field field, Object obj, String in) throws IllegalAccessException {

            }
        },
        Amount {
            @Override
            public String buildSetString(String in) {
                if (StringUtils.isBlank(in)) {
                    in = "0";
                }
                if (!in.matches("([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])")){
                    logger.error(String.format("错误的金额格式:%s", in));
                    return in;
                }
                return new BigDecimal(in).setScale(2, RoundingMode.HALF_UP).toString();
            }

            @Override
            public void buildSet(Field field, Object obj, String in) throws IllegalAccessException {
                if (StringUtils.isBlank(in)) {
                    in = "0";
                }
                field.set(obj, new BigDecimal(in).setScale(2, RoundingMode.HALF_UP));
            }
        },
        Int {
            @Override
            public String buildSetString(String str) {
                if (StringUtils.isBlank(str)) {
                    str = "0";
                }
                if (!str.matches("([1-9]\\d*|0$)")) {
                    logger.error(String.format("错误的数字格式:%s", str));
                    return str;
                }
                return str;
            }

            @Override
            public void buildSet(Field field, Object obj, String in) throws IllegalAccessException {
                if (StringUtils.isBlank(in)) {
                    in = "0";
                }
                field.set(obj, Integer.parseInt(in));
            }
        }

        ;

        public abstract String buildSetString(String str);
        public abstract void buildSet(Field field, Object obj, String in) throws IllegalAccessException;
    }
}
